-- 怪思陸離
local guaisiluli = fk.CreateSkill {
    name = "mcdh_guaisiluli",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

guaisiluli:addEffect("viewas",{
    prompt = "#mcdh_guaisiluli",
    pattern = "ice__slash",
    handly_pile = true,
    card_filter = function(self, player, to_select, selected)
        return #selected < 2
    end,
    view_as = function(self, player, cards)
        if #cards ~= 2 then return end
        local c = Fk:cloneCard("ice__slash")
        c.skillName = guaisiluli.name
        c:addSubcards(cards)
        return c
    end,
    before_use = function (self, player, use)
        local room = player.room
        local banner = room:getBanner(guaisiluli.name) or {}
        local index = 1
        if #banner > 0 then
            index = banner[#banner][1][1] + 1
        end
        use.extra_data = use.extra_data or {}
        use.extra_data.guaisiluli = index
        table.insert(banner, {{index}, {}})
        room:setBanner(guaisiluli.name, banner)
    end,
    after_use = function (self, player, use)
        local room = player.room
        local banner = room:getBanner(guaisiluli.name)
        local index = use.extra_data.guaisiluli
        if banner == nil or index == nil then return end
        local tos
        for i = #banner, 1, -1 do
            if banner[i][1][1] == index then
                tos = banner[i][2]
                table.remove(banner, i)
            end
        end
        room:setBanner(guaisiluli.name, #banner > 0 and banner or 0)
        if tos then
            tos = table.map(tos, Util.Id2PlayerMapper)
            if #tos > 0 then
                room:sortByAction(tos)
                for _, p in ipairs(tos) do
                    if not p.dead then
                        p:drawCards(2, guaisiluli.name)
                    end
                end
            end
        end
    end,
    enabled_at_response = function (self, player, response)
        return not response
    end,
})

guaisiluli:addEffect(fk.AfterCardsMove, {
    can_refresh = function(self, event, target, player, data)
        return player.room:getBanner(guaisiluli.name)
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local tos = {}
        for _, move in ipairs(data) do
            if move.from and move.to ~= move.from  and
                    table.find(move.moveInfo, function (info)
                        return Fk:getCardById(info.cardId).color == Card.Black and
                                (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip)
                    end) then
                table.insertIfNeed(tos, move.from)
            end
        end
        if #tos > 0 then
            local banner = room:getBanner(guaisiluli.name)
            for _, info in ipairs(banner) do
                table.insertTableIfNeed(info[2], table.map(tos, Util.IdMapper))
            end
            room:setBanner(guaisiluli.name, banner)
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_guaisiluli"] = "怪思陸離",
    [":mcdh_guaisiluli"] = "你可以将两张牌当冰【杀】使用，然后此流程中失去黑色牌的角色摸两张牌。",
    ["#mcdh_guaisiluli"] = "怪思陸離：将两张牌当冰【杀】使用，直到结算结束，期间失去过黑色牌的角色摸两张牌",
}

return guaisiluli